// const computedBehavior = require('miniprogram-computed')
const app = getApp();
const audioContext = app.globalData.audioContext;
Component({
  // behaviors: [computedBehavior],
  properties: {
    //音频列表
    list: {
      type: Array,
      value: [],

    },
    poster: {
      type: String,
      value: ""
    }
  },
  data: {
    flag: "play", //play 显示播放按钮；load 显示加载按钮；pause 显示暂停按钮
    index: 0,
    timeout: null
  },
  lifetimes: {
    attached() {


    },
    detached: function () {
      audioContext.stop()
    }
  },
  methods: {
    _monitor() {
      audioContext.onPlay(() => {
        console.log("--onPlay---")
        this.setData({
          flag: "pause"
        })
      })

      audioContext.onTimeUpdate(() => {
        console.log("--onTimeUpdate---")
        let {
          currentTime,
          duration
        } = audioContext;

        let changingTime = this.formatTime(currentTime)
        let maxTime = this.formatTime(duration)

        this.setData({
          currentTime,
          duration,
          changingTime,     //slider左侧时间
          maxTime           //slier右侧时间
        })
      })
      audioContext.onPause(() => {
        console.log("--onPause---")
        this.setData({
          flag: "play"
        })
      })

      audioContext.onWaiting(() => {
        console.log("--onWaiting---")
        this.setData({
          flag: "load"
        })
      })

      audioContext.onEnded(() => {
        console.log("--onEnded---")
        this.data.index++;
        this.play()
        this._monitor()
      })

      audioContext.onStop(() => {
        console.log("--onStop---")
        this.setData({
          flag: "play"
        })
      })
      audioContext.onError((err) => {
        console.log(err)
      })
    },
    //音频播放
    playByIndex(index) {

      this.data.index = index;
      this.play()

      this._monitor()

    },
    //播放当前
    play() {
      let {
        audioName,
        audioPath
      } = this.properties.list[this.data.index]

      this.setData({
        audioName
      })
      audioContext.src = audioPath;
      audioContext.coverImgUrl = this.properties.poster;
      audioContext.title = audioName;
      audioContext.play()
      // this.play()
    },
    //暂停播放
    pause() {
      audioContext.pause()
      this.setData({
        flag: "play"
      })
    },
    //下一个
    next() {
      var index = this.data.index + 1;
      this.playByIndex(index)
      this._monitor()
    },
    prev() {
      var index = this.data.index - 1;
      if (index < 0) index = 0;
      this.playByIndex(index)
      this._monitor()
    },
    //停止播放
    stop() {
      audioContext.stop()
      this.setData({
        flag: "play"
      })
    },
    //滑块滚动
    handleSlider(e) {
      var value = e.detail.value;
      audioContext.seek(value)
    },
    //点击大图
    posterClick() {
      wx.previewImage({
        urls: [this.properties.poster],
      })
    },
    //关闭播放器
    close() {
      audioContext.stop()
      this.triggerEvent("close")
    },
    //格式化时间
    formatTime(time) {
      let minute = Math.floor(time / 60);
      let second = Math.floor(time % 60);
      // console.log(minute,second,"4444")
      if (minute < 10) {
        minute = "0" + minute
      }
      if (second < 10) {
        second = "0" + second;
      }
      return minute + ":" + second
    },
    //定时自动关闭
    autoClose() {
      clearInterval(app.globalData.timer)
      var itemList = [
        "不开启", "15分钟", "30分钟", "60分钟", "90分钟"
      ]
      wx.showActionSheet({
        itemList,
        success: res => {
          let timeout = null; //单位秒
          if (res.tapIndex == 0) {
            clearInterval(app.globalData.timer)
            this.setData({
              timeout: null
            })
            return;
          }

          switch (res.tapIndex) {

            case 1:
              timeout = 15 * 60;
              break;
            case 2:
              timeout = 30 * 60;
              break;
            case 3:
              timeout = 60 * 60;
              break;
            case 4:
              timeout = 90 * 60;
              break;
          }
          if (timeout == 0) {
            clearTimeout(app.globalData.timer)
            return;
          }

          timeout--;
          this.setData({
            timeout: this.formatTime(timeout)
          })
          app.globalData.timer = setInterval(() => {
            timeout--;
            this.setData({
              timeout: this.formatTime(timeout)
            })
            if (timeout <= 0) {
              this.pause()
              this._monitor()
              clearTimeout(app.globalData.timer)
            }

          }, 1000);
        }
      })
    }


  }



})